package awesome.site.loader;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Controls execution of syncThread
 * 
 * @author Chris Carpenter
 * 
 */
public class SyncExecutorService {
	
	private ScheduledExecutorService executor;
	private SyncThread syncThread;
	private long delay;
	
	/**
	 * 
	 * @param _delay Time (in seconds) between executions.
	 * @param _syncThread Thread to execute.
	 */
	public SyncExecutorService(long _delay, SyncThread _syncThread) {
		this.syncThread = _syncThread;
		this.delay = _delay;
	}
	
	/**
	 * Begin executing.
	 */
	public void startExecutor() {
		//(re)initialize and start executor
		this.executor = Executors.newSingleThreadScheduledExecutor();
		executor.scheduleWithFixedDelay(this.syncThread , 0, delay, TimeUnit.SECONDS);
	}
	
	/**
	 * Stop executing.
	 */
	public void stopExecutor() {
		executor.shutdown();
	}
	
}
